<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/resources/js/jquery-1.11.1.min.js"></script>
    <script src="/resources/js/self.js"></script>
    <script type="text/javascript" src="/resources/js/md5.js"></script>
    <script type="text/javascript" src="/resources/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="/resources/js/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <link  href="/resources/viewer/viewer.css" rel="stylesheet">
    <script src="/resources/viewer/viewer.js"></script>
</head>
<body>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2>销售记录 <small></small></h2>
                <ul class="nav navbar-right panel_toolbox">

                    <li><button onclick="back()" class="btn btn-primary" type="button">返回</button>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <br>
                <div id="demo-form2"  class="form-horizontal form-label-left" >
                    <input type="hidden" name="id" class="form-data" >
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="deviceId">监控设备
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="deviceId" id="deviceId" class="form-control form-data">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">销售记录名称
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="name" name="name"
                                   class="form-control form-data col-md-7 col-xs-12" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="buyCompanyId">购买商
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="buyCompanyId" id="buyCompanyId" class="form-control form-data">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="timeBegin">开始时间
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="timeBegin" name="timeBegin" class="form-control form-data"
                                   onclick="WdatePicker({dateFmt:'yyyy-MM-dd  HH:mm:ss'})">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="timeEnd">结束时间
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="timeEnd" name="timeEnd" class="form-control form-data"
                                   onclick="WdatePicker({dateFmt:'yyyy-MM-dd  HH:mm:ss'})">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="amount">数量
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" id="amount" name="amount"
                                   class="form-control form-data col-md-7 col-xs-12" style="width:80% !important;">
                            <input type="text"  name="unit" readonly
                                   class="form-control form-data col-md-7 col-xs-12" style="width:20% !important;">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">审批说明
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <textarea class="form-control form-data" name="auditRemark" rows="3" placeholder=""></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">备注说明
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <textarea class="form-control form-data" name="replyRemark" rows="3" placeholder=""></textarea>
                        </div>
                    </div>
                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                            <button onclick="save()" class="btn btn-success">提交</button>
                            <!--<button onclick="back()" class="btn btn-primary" type="button">返回</button>-->
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    $(function () {
        buildSelect("/company/device/listAll.do","#deviceId","id","name",{});
        buildSelect("/company/buyCompany/listAll.do","#buyCompanyId","id","name",{});
        var device =   $("#deviceId").children('option:selected').attr("b-data");
        $("input[name='unit']").val(JSON.parse(device).product.unit);
        $("#deviceId").change(function () {
            var device = $(this).children('option:selected').attr("b-data");
            $("input[name='unit']").val(JSON.parse(device).product.unit);
        });
        initData();
    });

    function initData(){
        var data = getData();
        if(null2empty(data["id"]) !==""){
            var id = data.id;
            $("input[name='id']").val(id);
            $.ajax({
                url:"/company/saleOrder/find.do",
                data:{'id':id},
                type:'post',
                dataType:'json',
                success:function(data){
                    console.log(data);
                    if(data.success){
                        initPageDate(data.data, '.form-data');
                    }
                }
            });
            $("#account").attr('readonly','readonly');
        }

    }

    function save() {
        var params = getPageDataByClass("form-data");
        if (strIsNull(params['replyRemark'])) {
            alert("请输入备注说明");
            return;
        }
        $.ajax({
            url: "/company/saleOrder/remark.do",
            data: params,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                console.log(data);
                if (data.success) {
                    alert("保存成功");
                    var localData = getData();
                    goto("sale_order_list.html",{"process":localData.process});
                } else {
                    alert(data.msg);
                }
            }
        });
    }

    function uploadFile(fileType) {
        $.ajaxFileUpload({
            url: "/file/uploadFile.do?_t=" + new Date().getTime(),            //需要链接到服务器地址
            secureuri: false,//是否启用安全提交，默认为false
            fileElementId: fileType+'_input',                        //文件选择框的id属性
            dataType: 'text',                                     //服务器返回的格式，可以是json, xml
            success: function (data, status) {//服务器成功响应处理函数
                var rpeObj = $.parseJSON(data);
                var fileInfoList = rpeObj.data;
                addFileInfoToTable(fileInfoList,fileType);

            }
        });
    }
    function addFileInfoToTable(fileInfoList,fileType){
        $(fileInfoList).each(function(index,file){
            var fileHtml = "<div  style='border-radius: 2px; width:48%;height:399px;float: left; border: 2px #ffffff solid;'>"+
                "<div style='height:317px;overflow:hidden;'><img class='image' style='height:auto;width:100%;' src='"+getFileShowUrl(file.path)+"'></div>"+

                "<div align='center' style='height:80px;margin-bottom: 0px;'>"+
                "<div align='center' style='word-wrap:break-word'><input type='hidden' name='"+fileType+"_id' value='"+file.id+"'>"+file.name+"</div>"+


                "<button  style='margin-left: 20px;' name='delete' extendData='"+file.id+"'class='btn btn-primary'>删除</button><div>"
            "</div>";
            $(fileHtml).appendTo($("#"+fileType+"_div"));

        });
        $('.image').viewer();
        $("button[name='delete']").unbind('click');
        $("button[name='delete']").click(function(){
            var id = $(this).attr("extendData");
            $.ajax({
                type: "POST",
                url: "/file/delete.do?_t=" + new Date().getTime(),
                data: "fileId=" + id,

                success: function (result) {
                    alert("删除成功");
                },

                error: function (XMLHttpRequest, textStatus, thrownError) {
                    /* alert("系统异常");*/
                }
            });
            $(this).parent().parent().remove();
        });
    }
    //获取要展示的图片
    function getFileShowUrl(url){
        var extStart=url.lastIndexOf(".");
        var ext=url.substring(extStart,url.length).toUpperCase();
        if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){
            return "/resources/images/timg.jpg";
        }else{
            return url;
        }
    }
    function back() {
        var localData = getData();
        goto("sale_order_list.html",{"process":localData.process});
    }
</script>
</html>